package cn.hibernatedemo.biz;

import cn.hibernatedemo.dao.DeptDao;
import cn.hibernatedemo.dao.EmpDao;
import cn.hibernatedemo.dao.HibernateUtil;
import cn.hibernatedemo.entity.Dept;
import cn.hibernatedemo.entity.Emp;
import org.hibernate.Transaction;

import javax.swing.text.html.HTMLDocument;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class DeptBiz {
    private DeptDao deptDao=new DeptDao();
    public Dept getDeptById(Integer id){
        Transaction tx=null;
        Dept dept=null;
        try {
            tx= HibernateUtil.currentSession().beginTransaction();
            dept=deptDao.get(id);
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }
        return dept;
    }
    public void addDept(Dept dept){
        Transaction tx=null;

        try {
            tx= HibernateUtil.currentSession().beginTransaction();
            deptDao.add(dept);
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }

    }
    public void updDept(Dept dept){
        Transaction tx=null;

        try {
            tx= HibernateUtil.currentSession().beginTransaction();
            Dept dept1=deptDao.load(dept.getDeptNo());
            dept1.setDeptName(dept.getDeptName());
            dept1.setLocation(dept.getLocation());
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }

    }
    public void delDept(Integer id){
        Transaction tx=null;

        try {
            tx= HibernateUtil.currentSession().beginTransaction();
            Dept dept1=deptDao.load(id);
            deptDao.delete(dept1);
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }

    }

    public Iterator<Dept> getList( ){
        Transaction tx=null;
        Iterator<Dept> depts=null;
        try {
            tx= HibernateUtil.currentSession().beginTransaction();

            depts=deptDao.getList();
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }
        return depts;

    }

    public List<Dept> getDeptByname(String deptname){
        Transaction tx=null;
        List<Dept> depts=null;
        try {
            tx= HibernateUtil.currentSession().beginTransaction();

            depts=deptDao.findDeptName(deptname);
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }
        return depts;

    }

    public List<Dept> getDeptByObjs(Object[] objs){
        Transaction tx=null;
        List<Dept> depts=null;
        try {
            tx= HibernateUtil.currentSession().beginTransaction();

            depts=deptDao.findDeptObjs(objs);
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }
        return depts;

    }

    public List<Dept> getDeptByObj(Dept objs){
        Transaction tx=null;
        List<Dept> depts=null;
        try {
            tx= HibernateUtil.currentSession().beginTransaction();

            depts=deptDao.findDeptObj(objs);
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }
        return depts;

    }
    //调整员工所在的部门
    public void getDeptByObj(Integer empNo,Integer deptNo){
        Transaction tx=null;

        try {
            tx= HibernateUtil.currentSession().beginTransaction();
            Dept dept=new DeptDao().load(deptNo);
            Emp emp=new EmpDao().load(empNo);
            emp.setDept(dept);
            dept.getEmpSet().add(emp);
            tx.commit();

        }catch (Exception e){
            e.printStackTrace();
            if(tx!=null){
                tx.rollback();
            }
        }
    }
}
